/**
 * 智能搜索js
 */
var lucenceBda01 = "";// 智能搜索医嘱类型
var tapLable; //药品费药品不同的表头名称
//textarea在智能搜索的时候，回车添加数据的时候，屏蔽自动换行事件
function textareaStop(e){  
    var code;  
    if (!e) var  e = window.event;  
    if (e.keyCode) code = e.keyCode;  
    else if (e.which) code = e.which;  
    if(code==13 && window.event){  
        e.returnValue = false;  
    }else if(code==13){  
        e.preventDefault();  
    } 
}
function load_lucenceBda01()
{
	switch (topTab)
	{
		case 'Xiyao' :
			lucenceBda01 = "1,2";
			break;
		case 'Zhongyao' :
			lucenceBda01 = "3,8";
			break;
		case 'Jiancha' :
			lucenceBda01 = "E";
			break;
		case 'Jianyan' :
			lucenceBda01 = "L";
			break;
		case 'Shoushu' :
			lucenceBda01 = "S";
			break;
		case 'Zhiliao' :
			lucenceBda01 = "T,A,Z,N";
			break;
		case 'Cailiao' :
			lucenceBda01 = "4,M";
			break;
	}
}
function load_sorlBda01()
{
	switch (topTab)
	{
		case 'Xiyao' :
			lucenceBda01 = "(bda01:1 OR bda01:2)";
			tapLable = "药品名称" ;
			break;
		case 'Zhongyao' :
			lucenceBda01 = "(bda01:3 OR bda01:8)";
			tapLable = "药品名称" ;
			break;
		case 'Jiancha' :
			lucenceBda01 = "(bda01:E)";
			tapLable = "项目名称" ;
			break;
		case 'Jianyan' :
			lucenceBda01 = "(bda01:L)";
			tapLable = "项目名称" ;
			break;
		case 'Shoushu' :
			lucenceBda01 = "(bda01:S)";
			tapLable = "项目名称" ;
			break;
		case 'Zhiliao' :
			lucenceBda01 = "(bda01:T OR bda01:A OR bda01:Z OR bda01:N)";
			tapLable = "项目名称" ;
			break;
		case 'Cailiao' :
			lucenceBda01 = "(bda01:4 OR bda01:M)";
			tapLable = "项目名称" ;
			break;
	}
}
// 加载智能搜索框
function addSmartSearch(inputId)
{
	debugger;
	if($(".smart_search").is(":hidden")){
		
		activeAdviceInputId = inputId;// 更新激活当前激活的input框ID
		var screamH = $(".middle-container").height();
		var selefH = $(".smart_search").height();
		var thisLeft = $("#" + inputId).parent().offset().left;
		if(topTab == "Shoushu"){
			var thisTop = $("#" + inputId).offset().top;
			var thisheight = $("#" + inputId).outerHeight() + 1;
			
		}else{
			var thisTop = $("#" + inputId).parent().offset().top;
			var thisheight = $("#" + inputId).parents("td").outerHeight() + 1;
			
		}
		var showTop;
		if((thisTop<selefH)){
			showTop = thisTop + thisheight + "px"; //向下显示
			
		}else{
			showTop = thisTop  - selefH + "px"; //向上显示
			
		}
		$(".smart_search").slideDown();
		$(".smart_search").css(
				{
					// position:"absolute",
					left : thisLeft,
					top : showTop,
					display : "block"
				})
	}
	
	huanyixia(inputId);
	// smartSearchInput(inputId);
	
}

// 智能搜索input事件
function smartSearchInput(inputId)
{
	$("#" + inputId).on("input", function()
	{
		huanyixia(inputId);
	})
}

// solr 加载数据
function huanyixia(inputId)
{
	debugger;
	load_sorlBda01();
	var text = $("#" + inputId).val().replace(/[\r\n]/g,"");//获取到过滤换行符的字符串
	var reg= /^[A-Za-z]+$/; //判断是字母
	if (reg.test(text)){
		text = text.toLowerCase();
	}
	var url = rootUrl+"/solr/queryAdviceBySolr";
//	var url1 = "http://192.168.3.161:8089/solr/menzhenyisheng";
	var url1 = solrUrl + "/menzhenyisheng";
	var params={};
	if (topTab=="Xiyao")
	{
		params.q = "((bbx04:*"+text+"* OR szm:*" + text + "* OR bbx05:*" + text + "* OR abbrw:*" + text + "* OR abbrp:*"+text+"* OR bdk03:*"+text+"*) AND "
		+ lucenceBda01 + "  AND bck01a:"+bck01+" AND amount:[1 TO *]) ";
	}
	else if (topTab=="Zhongyao")
	{
		params.q = "((bbx04:*"+text+"* OR szm:*" + text + "* OR bbx05:*" + text + "* OR abbrw:*" + text + "* OR abbrp:*"+text+"* OR bdk03:*"+text+"*) AND "
		+ lucenceBda01 + "  AND bck01a:"+bck01+" AND amount:[1 TO *]) ";
	}
	else if (topTab=="Cailiao" && valueArr.p_9999_9999_106=="1")
	{
		params.q = "((bbx04:*"+text+"* OR szm:*" + text + "* OR bbx05:*" + text + "* OR abbrw:*" + text + "* OR abbrp:*"+text+"* OR bdk03:*"+text+"*) AND "
		+ lucenceBda01 + "  AND bck01a:"+bck01+"  AND amount:[1 TO *]) ";
	}
	else
	{
		params.q = "((bbx04:*"+text+"* OR szm:*" + text + "* OR bbx05:*" + text + "* OR abbrw:*" + text + "* OR abbrp:*"+text+"* OR bdk03:*"+text+"*) AND "
		+ lucenceBda01 + "  AND bck01a:"+bck01+") ";
	}
	params.fl = 'bbx04,bbx01,bby01,bda01,bbx05,bdg02,bby06,price,aas02,nbbx13,bby08,bag03,bag05,bck01,bck03';
	params.url = url1;
	params.sort = 'bbx05';
	params.rows = 30;
	params.yiyuanId = yiyuanId;
	$.ajax(
			{
				type : 'post',
				data:{params:JSON.stringify(params)},
				dataType : "json",
				url : url,
				success : function(msg)
				{
					debugger;
					var list = msg.list;
					$("#smart_search_table").empty();
					$("#smart_search_table").GridUnload();
					jQuery("#smart_search_table").jqGrid(
							{
								data : list,
								datatype : "local",
								height : 'auto',
								width : 'auto',
								autowidth : true,
								autoheight : true,
								rowNum:30,
								colModel : [
								           
								            {
								            	label : "编码",
								            	name : 'bbx04',
								            	align : "left",
								            	width : '110'
								            },
								            {
								            	label : tapLable,
								            	name : 'bbx05',
								            	align : "left",
								            	width: "300"
								            },
								            {
								            	name : 'bbx01',
								            	hidden : true,
								            },
								            {
								            	name : 'bby08',
								            	hidden : true,
								            },
								            {
								            	name : 'bby01',
								            	hidden : true,
								            },
								            {
								            	name : 'bda01',
								            	hidden : true,
								            },
								            {
								            	name : 'bag03',
								            	hidden : true,
								            },
								            {
								            	name : 'bag05',
								            	hidden : true,
								            },
								            {
								            	name : 'bck01',
								            	hidden : true,
								            },
								            {
								            	name : 'bck03',
								            	hidden : true,
								            },
								            {
								            	label : '执行科室',
								            	name : 'bck03',
								            	align : "center",
								            	hidden : (topTab=="Xiyao"||topTab=="Zhongyao"||topTab=="Cailiao")?false:true,
								            	width : '100px'
								            },
								            {
								            	label : '单位',
								            	name : 'bdg02',
								            	align : "center",
								            	width : '50px'
								            },
								            {
								            	label : '规格',
								            	name : 'bby06',
								            	align : "left",
								            	width : '100px'
								            },
								            {
								            	label : '单价',
								            	name : 'price',
								            	align : "right",
								            	width : '50px',
								            	formatter : function(cellvalue, options, rowObject)
								            	{
								            		return cellvalue?Math.floor(cellvalue * 100) / 100:""  ;
								            	}
								            },
								            {
								            	label : '保险类型',
								            	name : 'aas02',
								            	align : "center",
								            	width : '60px',
								            	cellattr : addCellAttr
								            },
								            {
								            	label : '项目特性',
								            	name : 'nbbx13',
								            	align : "left",
								            }
								            ],
								            ondblClickRow : function(rowid, status)
								            {
								            	var row = $("#smart_search_table").jqGrid("getRowData", rowid);
								            	row.type = 'treeTable';
								            	$(".smart_search").hide();
								            	activeAdviceInputId = inputId;
								            	// 赋值
								            	if ((topTab == "Xiyao") || (topTab == "Zhongyao")||(topTab == "Cailiao"))
								            	{
								            		debugger;
								            		addYaopin(row);
								            	}
								            	else
								            	{
								            		feiyaopin(row);
								            	}
								            	
								            	// 保存常用搜索
//			saveSmartSearch(row, inputId);
								            },
								            onSelectRow : function(rowid, status) {
//								    			loadYappinInfo(rowid);
								    		},
								            loadComplete : function(data)
								            {
								            	console.log("input");
								            	$("#smart_search_table").jqGrid("setSelection", 1);
								            }
							});
				},
				error:function(a,b,c)
				{
					console.log(c);
				}
			});
}

function addCellAttr(rowId, val, rowData, cm, rdata)
{
	switch (rowData["aas02"])
	{
		case '甲类' :
			return "style='color:#1fa9fd;";
			break;
		case '乙类' :
			return "style='color:#21cbc1;";
			break;
		case '丙类' :
			return "style='color:#a2d328;";
			break;
		case '自费' :
			return "style='color:#b079fc;";
			break;
		default :
			return "";
			break;
	}
}
// function smartSearchTr(inputId)
// {
// $("#smart_serach_table").find("tr").on("click", function()
// {
// var inputText = $(this).text();
// $(".smart_search").slideUp();
// $("#" + inputId).val(inputText);
// getDropList(inputId, inputText);
// saveSmartSearch(inputText);
// })
// }
// 保存常用搜索
function saveSmartSearch(row, inputId)
{
	var temp = [];
	temp.push(row);
	load_lucenceBda01();
	row.bda01 = lucenceBda01;
	row.username = $.cookie("loginName");
	var text = $("#" + inputId).val();
	$.ajax(
	{
	type : 'post',
	dataType : "json",
	data :
	{
		jsonList : JSON.stringify(temp)
	},
	url : rootUrl + "/doctoradvice/addLucenceIndex",
	success : function(msg)
	{
		console.log(msg.result);
	}
	});
}

function loadYappinInfo(id){
	var rowData = $("#smart_search_table").jqGrid("getRowData", id);
	var text =	rowData.bbx01;
	var url = rootUrl+"/solr/queryAdviceBySolr";
	
	var url1 = solrUrl + "/yaopincankao";
	var params={};
	params.q = "(bbx01:"+text+")";
	params.url = url1;
	
	$.when($.post(url, {params:JSON.stringify(params)})).done(function(data) {
		if (data.list.length > 0) {
			var info = data.list[0].bcw05;
			$(".smart_search").find("#yaopin_info").empty().append(info);
			
		}

	})
}
// function addUsefulSm()
// {
// load_lucenceBda01();
// var data1 =
// {
// bbx05 : text,
// bda01 : lucenceBda01,
// username : $.cookie("loginName")
// };
// $.ajax(
// {
// type : 'get',
// dataType : "json",
// data : data1,
// url : rootUrl + "/doctoradvice/queryBylucence",
// success : function(msg)
// {
// var list = msg.list;
// var html = "<table class='table table-striped table-border'
// id='smart_serach_table'>"
// for (var i = 0; i < list.length; i++)
// {
// html += "<tr><td style='text-align:left'>" + list[i].name + "</td></tr>"
// }
// html += "</table>";
// $(".smart_search").empty().append(html);
// smartSearchTr(inputId);
// }
// });
// }

///////////////////////////////////////////////////////////////////////////////------------------------------------------------------
//lucence加载数据
/*function huanyixia(inputId)
{

	load_lucenceBda01();
	var url = "";
	var text = $("#" + inputId).val();
	if (text.replace(/(^\s*)|(\s*$)/g, ""))
	{
		url = rootUrl + "/doctoradvice/queryBylucence"
	}
	else
	{
		url = rootUrl + "/doctoradvice/searchUserIndex"
	}
	var data1 =
	{
	bbx05 : text,
	bda01 : lucenceBda01,
	username : $.cookie("loginName")
	};
	$.ajax(
	{
	type : 'get',
	dataType : "json",
	data : data1,
	url : url,
	success : function(msg)
	{
		var list = msg.list;
		$("#smart_search_table").empty();
		$("#smart_search_table").GridUnload();
		jQuery("#smart_search_table").jqGrid(
		{
		data : list,
		datatype : "local",
		height : 'auto',
		width : 'auto',
		autowidth : true,
		autoheight : true,
		colModel : [
		{
		name : 'bbx01',
		hidden : true,
		},
		{
		name : 'bby01',
		hidden : true,
		},
		{
		name : 'bda01',
		hidden : true,
		},
		{
		label : '药品名称',
		name : 'bbx05',
		align : "left",
		},
		{
		label : '单位',
		name : 'bdg02',
		align : "center",
		width : '50px'
		},
		{
		label : '规格',
		name : 'bby06',
		align : "left",
		width : '100px'
		},
		{
		label : '单价',
		name : 'price',
		align : "right",
		width : '50px',
		formatter : function(cellvalue, options, rowObject)
		{
			return Math.floor(cellvalue * 100) / 100  ;
		}
		},
		{
		label : '保险类型',
		name : 'aas02',
		align : "center",
		width : '60px',
		cellattr : addCellAttr
		},
		{
		label : '项目特性',
		name : 'nbbx13',
		align : "left",
		}
		],
		ondblClickRow : function(rowid, status)
		{
			debugger;
			var row = $("#smart_search_table").jqGrid("getRowData", rowid);
			row.type = 'treeTable';
			$(".smart_search").hide();

			// 赋值
			if ((topTab == "Xiyao") || (topTab == "Zhongyao"))
			{
				addYaopin(row);
			}
			else
			{
				feiyaopin(row);
			}

			// 保存常用搜索
//			saveSmartSearch(row, inputId);
		},
		loadComplete : function(data)
		{
			$("#smart_search_table").jqGrid("setSelection", 1);
		}
		});
	}
	});
}*/